package com.lks.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.lks.common.controller.BaseController;
import com.lks.common.util.PageInfoUtil;
import com.lks.investor.pojo.AInvestor;
import com.lks.investor.pojo.AInvestorBank;
import com.lks.investor.service.IInvestorService;
import com.lks.users.pojo.AAdmins;

@Controller
@RequestMapping("/back/investor/")
public class InvestorBackController extends BaseController
{
	@Resource
	private IInvestorService investorService;

	/**
	 * 打开借贷方的信息页面
	 * 
	 * @return
	 */
	@RequestMapping("investorList.htm")
	public String investorList(HttpServletRequest request)
	{
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		String coopType = request.getParameter("coopType");
		if (keyword == null)
		{
			keyword = "";
		}
		if (status == null)
		{
			status = "";
		}
		if (st == null)
		{
			st = "";
		}
		if (ed == null)
		{
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;

		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);
		}

		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);

		List<AInvestor> investorList = this.investorService.findCondListInvestorService(pageInfoUtil, condMap);

		/* 存储结果 */
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		request.setAttribute("investorList", investorList);

		/* 存储条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		request.setAttribute("coopType", coopType);

		return "/back/investor/investorList";
	}

	/**
	 * 用户的的插入操作
	 * 
	 * @return
	 */
	@RequestMapping("/investorInsert.htm")
	public String investorInsert()
	{
		return "/back/investor/investorInsert";

	}

	/**
	 * 用户的添加的提交数据
	 * 
	 * @return
	 */
	@RequestMapping("/investorInsertSubmit.htm")
	public String investorInsertSubmit()
	{
		return "";
	}

	/**
	 * 用户的更新， 1.查询出个人信息， 2.将个人信息添加到其中的个人
	 * 
	 * @return
	 */
	@RequestMapping("/investorUpdate.htm")
	public String InvestorUpdate(HttpServletRequest request)
	{
		// 获取得到要修改数据的信息
		String iid = request.getParameter("iid");

		if (iid == null)
		{
			iid = "";
		}
		// 查询到investor中的id为iid的选项的数据的信息
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", Integer.parseInt(iid));
		AInvestor investor = this.investorService.findOneInvestorService(condMap);
		if (investor != null)
		{
			// 将其中的数据发送到页面中
			request.setAttribute("investor", investor);
			return "/back/investor/investorUpdate";
		}
		return "/back/investor/investorList";
	}

	/**
	 * 更新提交
	 * 
	 * 更新的时候不建议使用对象属性驱动 ~如果表中有30个字段;必须将所有的字段都写到表单中的name上;不写不赋值
	 * ~类型转换器;如果转换失败会报400错误
	 * 
	 * produces = "text/html;charset=UTF-8"
	 * response.setContextType("text/html;charset=UTF-8");
	 * 
	 * @return
	 */
	@RequestMapping(value = "/InvestorUpdateSubmit.htm", produces = "text/html;charset=UTF-8")
	public @ResponseBody String InvestorUpdateSubmit(String id, HttpServletRequest request, MultipartFile file)
	{
		// 先查询是否有这个参数
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AInvestor investor = this.investorService.findOneInvestorService(condMap);

		if (investor != null)
		{
			/* 接收参数 */
			String code = request.getParameter("code");
			String name = request.getParameter("name");
			String url = request.getParameter("url");
			String contactName = request.getParameter("contactName");
			String contactPhone = request.getParameter("contactPhone");
			String content = request.getParameter("content");
			String totalNum = request.getParameter("totalNum");
			String status = request.getParameter("status");
			String coopType = request.getParameter("coopType");

			String pubTime = request.getParameter("pubTime");

			investor.setPubTime(this.dateFormatUtil.parseDateTime(pubTime));
			// 封装到其中的对象中
			investor.setCode(code);
			investor.setName(name);
			investor.setUrl(url);
			investor.setContactName(contactName);
			investor.setContactPhone(contactPhone);
			investor.setTotalNum(Integer.valueOf(totalNum));
			investor.setContent(content);
			investor.setCoopType(Byte.valueOf(coopType));
			investor.setStatus(Byte.valueOf(status));
			investor.setUpdateTime(new Date());

			/* 单独处理其中的文件上传的file文件 */
			Map<String, Object> paramMap = new HashMap<String, Object>();
			// 长传文件信息
			paramMap.put("file", file);
			// 获取到其中的真实的路径
			paramMap.put("truePath", request.getSession().getServletContext().getRealPath("/"));
			try
			{
				JSONObject resultJSON = this.investorService.updateOneInvestorService(investor,paramMap);
				this.info = resultJSON.getString("info");
			} catch (Exception e)
			{
				this.info = "更新出资方的信息失败";
			}
		} else
		{
			this.info = "当前出资方不存在了";
		}
		// 返回的是一个JSONObject的对象
		JSONObject returnJuiJSON = this.returnJuiJSON(request, id);
		return returnJuiJSON.toString();
	}
}
